//
//  AY_BookSelfHeaderView.m
//  AiYue
//
//  Created by mac on 2023/10/16.
//

#import "AY_BookSelfHeaderView.h"
#import "AY_BookReadTimePage.h"
#import "AY_BookShelEditPage.h"
@interface AY_BookSelfHeaderView()

/** 时间 */
@property (strong,nonatomic) UILabel *timeLabel;
/** 历史 */
@property (strong,nonatomic) UIButton *historyBtn;
/** 管理 */
@property (strong,nonatomic) UIButton *setBtn;
/** 图 */
@property (strong,nonatomic) UIImageView *bookImg;
/** 书名 */
@property (strong,nonatomic) UILabel *bookName;
/** 简介 */
@property (strong,nonatomic) UILabel *bookDesc;
/** 章节 */
@property (strong,nonatomic) UILabel *bookChapter;

@end

@implementation AY_BookSelfHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.timeLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#646D75") andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:self];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(YNUM(20));
            make.top.equalTo(self).offset(YNUM(20));
        }];
        
        self.setBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"管理" andFont:YFont(14) andTitleColor:COLOR(@"#646D75") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(setAction) andRadius:0 andAddSubView:self];
        [self.setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-YNUM(16));
            make.centerY.equalTo(self.timeLabel);
        }];
        
        UIView *line = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"eeeeee") andRadius:0 andAddSubView:self];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.setBtn.mas_left).offset(-YNUM(15));
            make.centerY.equalTo(self.setBtn);
            make.width.mas_equalTo(1);
            make.height.mas_equalTo(YNUM(20));
        }];
        
        self.historyBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"阅读记录" andFont:YFont(14) andTitleColor:COLOR(@"#646D75") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(historyAction) andRadius:0 andAddSubView:self];
        [self.historyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(line.mas_left).offset(-YNUM(15));
            make.centerY.equalTo(self.timeLabel);
        }];
        
        self.bookImg = [AYTool createImageViewWithFrame:CGRectZero andImage:nil andBg:nil andRadius:YNUM(4) andAddSubView:self];
        [self.bookImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.timeLabel);
            make.top.equalTo(self.timeLabel.mas_bottom).offset(YNUM(20));
            make.width.mas_equalTo(YNUM(60));
            make.height.mas_equalTo(YNUM(80));
        }];
        
        self.bookName = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(16)] andLines:1 andRadius:0 andAddSubView:self];
        [self.bookName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookImg.mas_right).offset(YNUM(16));
            make.top.equalTo(self.bookImg).offset(YNUM(4));
            make.right.equalTo(self).offset(-YNUM(16));
        }];
        
        self.bookDesc = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#97A4B0") andBgColor:nil andFont:YFont(12) andLines:2 andRadius:0 andAddSubView:self];
        [self.bookDesc mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookImg.mas_right).offset(YNUM(16));
            make.top.equalTo(self.bookName.mas_bottom).offset(YNUM(6));
            make.right.equalTo(self).offset(-YNUM(16));
        }];
        
        self.bookChapter = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#646D75") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self];
        [self.bookChapter mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bookImg.mas_right).offset(YNUM(16));
            make.top.equalTo(self.bookDesc.mas_bottom).offset(YNUM(6));
            make.right.equalTo(self).offset(-YNUM(16));
        }];
        
        UIView *line1 = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"f6f6f6") andRadius:0 andAddSubView:self];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.timeLabel);
            make.right.equalTo(self.setBtn);
            make.height.mas_equalTo(1);
            make.bottom.equalTo(self);
        }];
        
        UIButton *btn = [[UIButton alloc] init];
        [btn addTarget:self action:@selector(read) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.top.equalTo(self.bookImg);
        }];
    }
    return self;
}

- (void)read {
    AY_PageReadViewController *vc = [[AY_PageReadViewController alloc] init];
    [vc sendBookId:self.model.articleId andPage:self.model.pageNo.integerValue - 1 <= 0 ? @"0" : [NSString stringWithFormat:@"%ld",self.model.pageNo.integerValue - 1] andChapterIndex:[NSString stringWithFormat:@"%ld",self.model.chapterNo.integerValue - 1]];
    [[AYTool viewControllerWith:self].navigationController pushViewController:vc animated:YES];
}

- (void)setModel:(BookSelfModel *)model {
    _model = model;
    [self.bookImg sd_setImageWithURL:[NSURL URLWithString:model.articleCoverImg] placeholderImage:YImage(@"placeholderImage")];
    self.bookDesc.text = model.articleDescription;
    self.bookName.text = model.articleTitle;
    self.bookChapter.text = model.chapterNo.integerValue <= 0 ? [NSString stringWithFormat:@"1/%@",model.chapterCount] : [NSString stringWithFormat:@"%@/%@",model.chapterNo,model.chapterCount];
}

- (void)setReadTime:(NSString *)readTime {
    NSInteger hourNum = readTime.integerValue/3600;
    NSInteger minuteNum = readTime.integerValue%3600/60;
    if (hourNum == 0) {
        self.timeLabel.text = [NSString stringWithFormat:@"今日已阅读%ld分钟",minuteNum];
    }else{
        self.timeLabel.text = [NSString stringWithFormat:@"今日已阅读%ld时%ld分",hourNum,minuteNum];
    }
}

- (void)setDataAry:(NSMutableArray *)dataAry {
    _dataAry = dataAry;
}


#pragma mark - 管理
- (void)setAction {
    AY_BookShelEditPage * page = [[AY_BookShelEditPage alloc] init];
    page.dataSource = self.dataAry;
    page.shelfVc = self.shelfVc;
    page.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [[AYTool viewControllerWith:self].navigationController presentViewController:page animated:YES completion:nil];

}

#pragma mark - 历史
- (void)historyAction {
    AY_BookReadTimePage * page = [[AY_BookReadTimePage alloc] init];
    [[AYTool viewControllerWith:self].navigationController pushViewController:page animated:YES];
}


@end
